<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 彩种逻辑
 */
class Lottery extends AdminBase
{
    
    /**
     * 彩种分类编辑
     */
    public function LotteryTypeEdit($data = [])
    {

        $validate_result = $this->validateLotteryType->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateLotteryType->getError()];
        }
        $url = url('LotteryCategoryList');

        
        $result = $this->modelLotteryType->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '彩种分类' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelLotteryType->getError()];
    }
    
    /**
     * 获取彩种列表
     */
    public function getLotteryList($where = [], $field = 'a.*,m.nickname,c.name as category_name', $order = '')
    {
        
        $this->modelLottery->alias('a');
        
        $join = [

                    [ '__LOTTERY_TYPE__ c', 'a.cate_id = c.id'],
                ];

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];

        $this->modelLottery->join = $join;
        $list=$this->modelLottery->getList($where, $field, $order);

        $this->modelLottery->alias('a');
        $count=$this->modelLottery->join('__LOTTERY_TYPE__ c', 'a.cate_id = c.id')->where($where)->count();
        return ['list'=>$list,'count'=>$count];
    }
    
    /**
     * 获取彩种列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.name|a.describle'] = ['like', '%'.$data['search_data'].'%'];
        !empty($data['cate_id'])  && $where['a.cate_id'] =$data['cate_id'];
        return $where;
    }
    
    /**
     * 彩种信息编辑
     */
    public function LotteryEdit($data = [])
    {

        $validate_result = $this->validateLottery->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateLottery->getError()];
        }
        $url = url('LotteryList');


        $result = $this->modelLottery->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '彩种' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '彩种操作成功', $url] : [RESULT_ERROR, $this->modelLottery->getError()];
    }

    /**
     * 获取彩种信息
     */
    public function getLotteryInfo($where = [], $field = 'a.*,c.name as category_name')
    {
        
        $this->modelLottery->alias('a');

        $join = [

            [ '__LOTTERY_TYPE__ c', 'a.cate_id = c.id'],
        ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelLottery->join = $join;
        
        return $this->modelLottery->getInfo($where, $field);
    }
    
    /**
     * 获取分类信息
     */
    public function getLotteryTypeInfo($where = [], $field = true)
    {
        
        return $this->modelLotteryType->getInfo($where, $field);
    }
    
    /**
     * 获取彩种分类列表
     */
    public function getLotteryTypeList($where = [], $field = true, $order = '', $paginate = 0)
    {
                $count=$this->modelLotteryType->where($where)->count();
                $list=$this->modelLotteryType->getList($where, $field, $order, $paginate);
                return ['count'=>$count,'list'=>$list];
    }
    
    /**
     * 彩种分类删除
     */
    public function LotteryTypeDel($where = [])
    {
        
        $result = $this->modelLotteryType->deleteInfo($where);
        
        $result && action_log('删除', '彩种分类删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '彩种分类删除成功'] : [RESULT_ERROR, $this->modelLotteryType->getError()];
    }
    
    /**
     * 彩种删除
     */
    public function LotteryDel($where = [])
    {
        
        $result = $this->modelLottery->deleteInfo($where);
        
        $result && action_log('删除', '彩种删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '彩种删除成功'] : [RESULT_ERROR, $this->modelLottery->getError()];
    }

    /**
     * 彩种删除
     */
    public function update_info($where = [])
    {

        //dd($where);

        $result = $this->modelLottery->setInfo($where);


        return $result ? [RESULT_SUCCESS, '操作成功'] : [RESULT_ERROR, '操作失败'];
    }
}
